import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { ObjCreate } from "../../common/ObjCreate";
import { Game } from "../../../Game";
import { PetExploreEntityList } from "../PetExploreEntityList";
import { PetExploreMainAvatar } from "./PetExploreMainAvatar";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { ResFileConst } from "../../../resource/ResFileConst";
import { PetExploreCardsPanel } from "./PetExploreCardsPanel";
import { PetExploreConfigInfo } from "../../../resource/info/PetExploreConfigInfo";
import { SPetExploreCards } from "../../../../../Message/Public/SPetExploreCards";
import { Cache } from "../../../cache/Cache";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { PetExploreUtil } from "./info/PetExploreUtil";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { DataEvent } from "../../../events/DataEvent";
import { PlayerEvent } from "../../../scene/events/PlayerEvent";
import { SEntityInfo } from "../../../../../Message/BroadCast/SEntityInfo";
import { EEntityType } from "../../../../../Message/Public/EEntityType";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { IEntity } from "../../../scene/player/IEntity";
type int = number;
//class PetExploreBattleWindow
    
    export  class PetExploreBattleWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this.setSize(695, 475);
            this.titleHeight = 28;
            this.title = Language.getString(54461);
            Dispatcher.addEventListener(EventName.PetExploreEntityUpdate, this.onPetExploreEntityUpdateHandler);
            Dispatcher.addEventListener(EventName.PetExploreMonsterClean, this.petExploreMonsterClean);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            ObjCreate.createBg(17, 39, 660, 425, this);
            this._mapBitmap = new Object /* flash.display.Bitmap */();
            this._mapBitmap.x = 21;
            this._mapBitmap.y = 41;
            this.addChild(this._mapBitmap);
            Game.petEntity.addToStage(this);
            Game.petEntity.x = 21;
            Game.petEntity.y = 41;
            this._petList = new PetExploreEntityList();
            this._petList.x = 25;
            this._petList.y = 45;
            this.addChild(this._petList);
            this._petAvatar = new PetExploreMainAvatar();
            this._petAvatar.showViewBtn(true);
            this._petAvatar.x = 65;
            this._petAvatar.y = 45;
            this.addChild(this._petAvatar);
            this._enemyAvatar = new PetExploreMainAvatar();
            this._enemyAvatar.isScale = true;
            this._enemyAvatar.showViewBtn(false);
            this._enemyAvatar.x = 440;
            this._enemyAvatar.y = 45;
            this._enemyAvatar.setLifePercent(true);
            this.addChild(this._enemyAvatar);
            this._levelTxt = ObjCreate.createTextField("", 0, 50, 695, 30, this, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            this._levelTxt.filters = [FilterConst.vipGlowFilter];
            this._levelTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._nameTxt = ObjCreate.createTextField("", 0, 75, 695, 30, this, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._enemyList = new PetExploreEntityList();
            this._enemyList.x = 640;
            this._enemyList.y = 45;
            this.addChild(this._enemyList);
            this._exitBattleBtn = ObjCreate.createGLoadedButton(ResFileConst.ExitWatch, 285, 415, 121, 40, this);
            this._exitBattleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onExitBattleBtnClickHandler);
            this._exitBattleBtn.toolTipData = Language.getString(54462);
            _closeBtn.toolTipData = Language.getString(54462);
            this._cardsPanel = new PetExploreCardsPanel();
            this._cardsPanel.x = 650;
            this._cardsPanel.y = 340;
            this._cardsPanel.tip = Language.getString(54547);
            this.addChild(this._cardsPanel);
            return;
        }

        public updateByPetExploreConfigInfo(arg1: PetExploreConfigInfo): void
        {
            if (arg1) 
            {
                this._levelTxt.htmlText = Language.getStringByParam(54594, arg1.level);
                this._nameTxt.htmlText = arg1.name;
            }
            return;
        }

        public showCardPanel(arg1: Boolean): void
        {
            this._cardsPanel.showPanel(arg1);
            return;
        }

        public updateCardsPanel(arg1: SPetExploreCards): void
        {
            this._cardsPanel.updateCards(arg1.cards);
            return;
        }

        /* internal  */updateMap(arg1: int): void
        {
            var area: int;

            var loc1;
            area = arg1;
            if (area != Cache.instance.petExplore.preLoadArea) 
            {
                Game.petEntity.petLayer.removeAllChildren();
                this._currentMonsterIndex = 0;
            }
            LoaderManager.instance.load(PetExploreUtil.getAreaMapName(area), (arg1: ImageInfo): void
            {
                _mapBitmap.bitmapData = arg1.bitmapData;
                return;
            })
            Cache.instance.petExplore.preLoadArea = area;
            return;
        }

        /* internal  */onExitBattleBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            this.hide();
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            var loc2=0;
            super.show(arg1, arg2);
            var loc1=Cache.instance.petExplore.selectedConfigInfo;
            this._petList.updateDataProvider(Cache.instance.petExplore.petExploreInfo.petUids);
            if (loc1) 
            {
                loc2 = loc1.area;
                this.updateMap(loc2);
                this._enemyList.updateDataProvider(loc1.bossInfoArray);
            }
            this.updateCardsPanel(Cache.instance.petExplore.skillCards);
            ThingUtil.sceneEntityUtil.removeEntityBySceneId(Cache.instance.petExplore.mapId);
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.exitWatch();
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreOpen, false));
            return;
        }

        /* internal  */exitWatch(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreWatch, false));
            return;
        }

        public removeAllPetByScene(arg1: int): void
        {
            if (this._monsterIEntity) 
            {
                this._monsterIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
            }
            if (this._petIEntity) 
            {
                this._petIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
            }
            return;
        }

        /* internal  */onPetExploreEntityUpdateHandler(arg1: DataEvent): void
        {
            this._isPet = arg1.data["isPet"] as Boolean;
            var loc1=arg1.data["entityInfo"] as SEntityInfo;
            if (this._isPet) 
            {
                this._petAvatar.updateAvatarUI(loc1, true);
                if (this._petIEntity) 
                {
                    this._petIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
                }
                this._petIEntity = ThingUtil.sceneEntityUtil.getEntity(loc1.entityId, Cache.instance.petExplore.selectedConfigInfo.map);
                if (this._petIEntity) 
                {
                    this._petIEntity.addEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
                }
            }
            else 
            {
                if (this._monsterIEntity) 
                {
                    this._monsterIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
                }
                this._monsterIEntity = ThingUtil.sceneEntityUtil.getEntity(loc1.entityId, Cache.instance.petExplore.selectedConfigInfo.map);
                if (this._monsterIEntity) 
                {
                    this._monsterIEntity.addEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
                }
                this._enemyAvatar.updateAvatarUI(loc1, false);
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreMonsterIndexUpdate, this._currentMonsterIndex));
            }
            return;
        }

        /* internal  */petExploreMonsterClean(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1 && this._entityInfo) 
            {
                this._entityInfo.life = 0;
                this._enemyAvatar.updateAvatarUI(this._entityInfo, false);
            }
            return;
        }

        /* internal  */onPetEntityInfoUpdateHandler(arg1: PlayerEvent): void
        {
            var loc2=null;
            var loc1=arg1.player;
            if (loc1) 
            {
                loc2 = loc1.entityInfo.entityInfo;
                if (!(loc2.msgEx == null) && loc2.entityId && loc2.entityId.type == EEntityType._EEntityTypePetInExplore) 
                {
                    this._petAvatar.updateAvatarUI(loc2, true);
                    if (loc1.isDead) 
                    {
                        this._petIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
                    }
                }
            }
            return;
        }

        /* internal  */onMonsterEntityInfoUpdateHandler(arg1: PlayerEvent): void
        {
            var loc1=null;
            var loc2=arg1.player;
            if (loc2 && loc2.entityInfo) 
            {
                this._entityInfo = loc2.entityInfo.entityInfo;
                if (this._entityInfo && !(this._entityInfo.msgEx == null)) 
                {
                    this._enemyAvatar.updateAvatarUI(this._entityInfo, false);
                    if (loc2.isDead) 
                    {
                        this._monsterIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
                    }
                }
            }
            return;
        }

        /* internal */ /* var */_mapBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_petAvatar: PetExploreMainAvatar;

        /* internal */ /* var */_enemyAvatar: PetExploreMainAvatar;

        /* internal */ /* var */_levelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_petList: PetExploreEntityList;

        /* internal */ /* var */_enemyList: PetExploreEntityList;

        /* internal */ /* var */_exitBattleBtn: GLoadedButton;

        /* internal */ /* var */_cardsPanel: PetExploreCardsPanel;

        /* internal */ /* var */_battlePetInfo: SPetInfo;

        /* internal */ /* var */_petIEntity: IEntity;

        /* internal */ /* var */_monsterIEntity: IEntity;

        /* internal */ /* var */_isPet: Boolean;

        /* internal */ /* var */_currentMonsterIndex: int;

        /* internal */ /* var */_entityInfo: SEntityInfo;
    }
